package com.scs.application.core.model;

import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
import cn.afterturn.easypoi.handler.inter.IExcelModel;
import com.scs.application.core.thread.DefaultSaveHandler;
import com.scs.application.core.utils.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 批量导入结果
 *
 **/
@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ImportResult implements Serializable {
    /**
     * 导入的总行数
     */
    private int totalCount;

    /**
     * 导入成功行数
     */
    private int successCount;

    /**
     * 导入失败行数
     */
    private int failCount;


    /**
     * 耗时，单位：秒
     */
    private double elapsedSecond;

    /**
     * 失败原因集合
     */
    private List<String> messages;


    public static ImportResult build(double totalTimeSeconds, int listSize, DefaultSaveHandler handler) {
        ImportResult result = new ImportResult();
        result.setTotalCount(listSize)
                .setSuccessCount(handler.getSuccessCount())
                .setFailCount(handler.getFailureCount())
                .setElapsedSecond(totalTimeSeconds)
                .setMessages(handler.getMessageList());

        log.info("本次导入共处理{}条数据，成功：{}条,失败：{},耗时：{}秒",
                result.getTotalCount(), result.getSuccessCount(), result.getFailCount(), result.getElapsedSecond());
        return result;
    }

    public static ImportResult build(double totalTimeSeconds, cn.afterturn.easypoi.excel.entity.result.ExcelImportResult importResult) {

        List successList = importResult.getList();
        List failList = importResult.getFailList();

        ImportResult result = new ImportResult();
        result .setElapsedSecond(totalTimeSeconds);
        if (successList != null && !successList.isEmpty()) {
            result.setTotalCount(successList.size() + failList.size()).setSuccessCount(successList.size());
        }
        if (failList != null && !failList.isEmpty()) {
            result.setTotalCount(failList.size())
                    .setFailCount(failList.size());
        }

        if (failList != null && !failList.isEmpty()) {
            Object obj = failList.get(0);
            if (obj instanceof IExcelDataModel && obj instanceof IExcelModel) {
                result.setMessages(
                        (List<String>) importResult.getFailList().stream().map(o ->
                                MessageFormat.format("第{0}行 {1}", ((IExcelDataModel) o).getRowNum() + 1, ((IExcelModel) o).getErrorMsg())
                        ).collect(Collectors.toList())
                );
            }
        }

        log.info("本次导入共处理{}条数据，成功：{}条,失败：{},耗时：{}秒",
                result.getTotalCount(), result.getSuccessCount(), result.getFailCount(), result.getElapsedSecond());
        log.info(StringUtils.join(result.getMessages(), "\n"));
        return result;
    }
}

